﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms
{
    public class BFilterPanelDesigner : System.Windows.Forms.Design.ParentControlDesigner
    {

        private DesignerVerbCollection verbCollection;
        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (verbCollection is null)
                {
                    verbCollection = new DesignerVerbCollection();
                    verbCollection.Add(new DesignerVerb("创建控件", createControls));
                }
                return verbCollection;
            }
        }

        private void createControls(object sender, EventArgs e)
        {
            FilterPanel filterPanel = this.Control as FilterPanel;
            Marmat.Forms.Design.BFilterPanelForm form = new Design.BFilterPanelForm(filterPanel);
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int column = 0;
                int columnCount = (int)form.BNumericBox1.Value;
                int left = 12;
                int top = 12;
                foreach (Marmat.Forms.Views.DataGrid.Row row in form.BDataGrid1.Rows)
                {
                    if (true.Equals(row["选择"]) && !row.IsNull("控件类型"))
                    {
                        Type type = (Type)row["控件类型"];
                        object ct = type.Assembly.CreateInstance(type.FullName);
                        if (ct is Control control)
                        {
                            this.Control.FindForm().Container.Add(control);
                            control.Location = new System.Drawing.Point(left, top);
                            filterPanel.Controls.Add(control);
                            if (ct.GetType().GetProperty("Caption") != null)
                            {
                                ct.GetType().GetProperty("Caption").SetValue(ct, row["标题"].ToString() + ":", null);
                            }
                            filterPanel.SetColName(control, row["字段"].ToString());
                            filterPanel.SetDataType(control, row["数据类型"] as Type);
                            column++;
                            left += control.Width + 12;
                            if (column == columnCount)
                            {
                                column = 0;
                                left = 12;
                                top += control.Height + 6;
                            }
                        }
                    }
                }

                if (form.CheckBox1.Checked)
                {
                    DataGrid dataGrid = form.BComboBox1.SelectedValue as DataGrid;
                    filterPanel.Table = dataGrid;
                    foreach (string name in new string[] { "提交", "清除" })
                    {
                        Button button = new Button();
                        button.Text = name;
                        this.Control.FindForm().Container.Add(button);
                        this.Control.Controls.Add(button);
                        //if (name == "提交")
                        //    filterPanel.FindButton = button;
                        //else
                        //    filterPanel.ClearButton = button;
                        button.Location = new System.Drawing.Point(left, top);
                        column++;
                        left += button.Width + 12;
                        if (column == columnCount)
                        {
                            column = 0;
                            left = 12;
                            top += button.Height + 6;
                        }
                    }
                }
            }
        }
    }
}
